<template>
  <div>
    <FixedHeightVue>
      <template v-if="userInfo.roleType == 0">
        <!-- 数据统计页 -->
        <div class="gird">
          <div class="home-card-item">
            <!-- 动画 -->
            <div class="animation_top_left"></div>
            <div class="animation_bottom_right"></div>
            <div class="home-card-item-left home-one-animation0">
              <div class="home-card-item-left-top">
                <div class="home-card-item-left-top-left">{{ statisticsTopData.dayLoginNumber }}</div>
                <div class="home-card-item-left-top-right">-12.33%</div>
              </div>
              <div class="home-card-item-left-bottom">今日用户登录数</div>
            </div>
            <div class="home-card-item-right home-one-animation0">
              <img src="@/assets/drxx92.png" alt="">
            </div>
          </div>
          <div class="home-card-item ">
            <div class="home-card-item-left home-one-animation1">
              <div class="home-card-item-left-top">
                <div class="home-card-item-left-top-left">{{ statisticsTopData.dayReleaseNumber }}</div>
                <div class="home-card-item-left-top-right">-12.33%</div>
              </div>
              <div class="home-card-item-left-bottom ">
                今日作业发布数量
              </div>
            </div>
            <div class="home-card-item-right home-one-animation1">
              <img src="@/assets/cehuifabupiliangfabu.png" alt="">
            </div>
          </div>
          <div class="home-card-item ">
            <div class="home-card-item-left home-one-animation2">
              <div class="home-card-item-left-top">
                <div class="home-card-item-left-top-left">{{ statisticsTopData.daySubmitNumber }}</div>
                <div class="home-card-item-left-top-right">-12.33%</div>
              </div>
              <div class="home-card-item-left-bottom">学生提交数据</div>
            </div>
            <div class="home-card-item-right home-one-animation2">
              <img src="@/assets/zuoye.png" alt="">
            </div>
          </div>
        </div>
        <!-- 图表 -->
        <div class="echarts_box">
          <div id="echart"></div>
          <div class="quick_navigation">
            <div class="quick_navigation_title">快捷导航栏</div>
            <div class="quick_navigation_box">
              <div class="quick_navigation_box_item" v-for="item, index in navigation" :key="index"
                :class="'x-animation' + index">
                <div class="icon iconfont" :class="item.icon"></div>
                <div class="title">{{ item.name }}</div>
              </div>
            </div>
          </div>
        </div>

      </template>
      <StudentHome v-else />
    </FixedHeightVue>
  </div>
</template>

<script setup>
import FixedHeightVue from "@/components/base/FixedHeight.vue";
import { ref, onMounted } from 'vue'
import StudentHome from '@/views/home/StudentHome'

import useUserInfo from "store/useUserInfo.js";
const useUserInfoStore = useUserInfo();
import { storeToRefs } from 'pinia'
const { userInfo } = storeToRefs(useUserInfoStore)

import { ElMessage } from "element-plus";
import { getAdminStatisticsApi } from "@/api/index";


import * as echarts from 'echarts';


// 快捷导航栏数据
const navigation = ref([
  {
    id: 1,
    name: '学生提交数据',
    icon: 'icon-jubao',
  },
  {
    id: 1,
    name: '学生提交数据',
    icon: 'icon-jubao',
  },
  {
    id: 1,
    name: '学生提交数据',
    icon: 'icon-jubao',
  },
  {
    id: 1,
    name: '学生提交数据',
    icon: 'icon-jubao',
  },
  {
    id: 1,
    name: '学生提交数据',
    icon: 'icon-jubao',
  },
  {
    id: 1,
    name: '学生提交数据',
    icon: 'icon-jubao',
  },
  {
    id: 1,
    name: '学生提交数据',
    icon: 'icon-jubao',
  },
])
// 管理员顶部数据
const statisticsTopData = ref({
  dayLoginNumber: 0,
  dayReleaseNumber: 0,
  daySubmitNumber: 0,
  loginList: [],
  releaseList: [],
  submitList: [],
})

// 获取管理员首页数据
const getAdminStatistics = async () => {
  const res = await getAdminStatisticsApi({
    userId: userInfo.value.userId
  });
  if (res.code !== "10000") {
    ElMessage({
      showClose: true,
      message: res.message,
      type: "error",
    })
    return;
  }
  statisticsTopData.value = Object.assign(statisticsTopData.value, res.data);
  myChartFun()
}

// 最近七天时间
const time = ref([])
const commentTime = () => {
  const date = new Date()
  const day = date.getDate()
  const month = date.getMonth()
  const year = date.getFullYear()
  for (let i = 0; i < 7; i++) {
    time.value.push(new Date(year, month, day - i).toLocaleDateString())
  }
}


// 初始化echarts实例
const myChartFun = () => {
  // echarts配置
  const option = {
    xAxis: {
      type: 'category',
      data: time.value.reverse()
    },
    legend: {
      type: 'plain',
      right: 30,
      top: 20
    },
    yAxis: {
      type: 'value',
      axisLine: {
        show: true,
        symbol: ['none', 'arrow'],//只在末端显示箭头
        lineStyle: {
          type: 'solid',
          color: '#409eff',
          // ...
        }
      }
    },
    tooltip: {
      trigger: "item",
      axisPointer: {
        type: "cross"
      }
    },
    series: [
      {
        name: '用户登录数',
        data: statisticsTopData.value.loginList,
        type: 'line',
        smooth: true
      },
      {
        name: '作业发布数',
        data: statisticsTopData.value.releaseList,
        type: 'line',
        smooth: true
      },
      {
        name: '作业提交数',
        data: statisticsTopData.value.submitList,
        type: 'line',
        smooth: true
      }
    ]
  };

  const myChart = echarts.init(document.getElementById('echart'));
  option && myChart.setOption(option);
}

onMounted(() => {
  if (userInfo.value.roleType == 0) {
    commentTime()
    // myChartFun()
    getAdminStatistics()
  }

})

</script>

<style scoped lang="scss">
.gird {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr;
  gap: 15px;
  grid-template-areas: "a a a"
    "b b b";

  .home-card-item {
    background-color: #fff;
    padding: 10px 20px;
    border-radius: 10px;
    height: 120px;
    display: flex;
    align-items: center;
    position: relative;



    &:hover {
      box-shadow: 0 2px 12px #0000001a;
      transition: all ease 0.3s;
    }

    &-left {
      flex: 2;

      &-top {
        display: flex;
        align-items: end;

        &-left {
          font-size: 30px;
          color: #303133;
        }

        &-right {
          margin-left: 10px;
          font-size: 16px;
          /* font-weight: 600; */
          color: rgb(255, 100, 98);

        }
      }

      &-bottom {
        font-size: 16px;
        color: #303133;
        margin-top: 10px;
      }
    }

    &-right {
      /* flex: 1; */
      display: inline-block;
      width: 100px;
      height: 100px;
      border-radius: 50%;

      /* background-color: #fdf6ec; */
      img {
        width: 100%;
        height: 100px;
      }
    }
  }
}

/* 动画 */
@for $i from 0 through 2 {
  .home-one-animation#{$i} {
    opacity: 0;
    animation-name: error-num;
    animation-duration: 0.5s;
    animation-fill-mode: forwards;
    animation-delay: calc($i/4) + s;
  }
}


@for $i from 0 through 10 {
  .x-animation#{$i} {
    opacity: 0;
    animation-name: translate-num;
    animation-duration: 0.8s;
    animation-fill-mode: forwards;
    animation-delay: calc($i/5) + s;
  }
}

.echarts_box {
  /* background-color: #fff; */
  height: calc(100% - 135px);
  display: flex;
  flex-direction: column;

  #echart {
    flex: 3;
    background-color: #fff;
    margin-bottom: 10px;
  }

  .quick_navigation {
    flex: 1;
    background-color: #fff;

    &_title {
      padding: 10px;
      font-size: 15px;
      font-weight: 700;
    }

    &_box {
      display: grid;
      grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
      grid-gap: 50px;
      padding: 10px;


      &_item {
        /* background-color: aquamarine; */
        width: 100px;
        height: 100px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        align-items: center;
        border: 1px solid #eee;
        cursor: pointer;

        &:hover {
          box-shadow: 0 2px 12px #0000001a;
          transition: all ease 0.3s;
        }

        .icon {
          font-size: 60px;
          color: #409eff;
        }

        .title {
          font-size: 14px;
          font-weight: 500;
          color: #000;
          line-height: 22px;
          margin-top: 5px;
        }
      }
    }
  }
}
</style>
